package game.worlds;

import game.script.AiScript;
import game.views.FightStateView;

/**
 * 基础的1V1角色逻辑
 */
class Game1V1BaseWorld extends GameWorld {
	/**
	 * 1p是否开启AI
	 */
	public var p1AiEnable:Bool = false;

	/**
	 * 2p是否开启AI
	 */
	public var p2AiEnable:Bool = false;

	override function onRoleCreate() {
		super.onRoleCreate();
		var fightState:FightStateView = cast this.state;

		// 旧角色
		var roleA = fightState.p1.role;
		var roleB = fightState.p2.role;

		/**s
		 * 创建一个角色
		 */
		var role = new GameRole(this, gameAssets.roles.get(this.option.player1));
		role.name = "p1";
		role.troop = 0;
		role.body.x = mapWidth / 2 - 150;
		role.body.y = maxWorldY + 5;
		this.roleBoids.push(role.body);
		this.addDisplay(role);

		if (roleA != null && roleA.data.hp > 0) {
			// 保留HP MP
			role.data = roleA.data;
			if (role.roleid != roleA.roleid) {
				role.switchRole(role.roleid);
			}
		}

		fightState.p1.bindRole(role);
		if (p1AiEnable) {
			role.aiScript = new AiScript();
			role.aiScript.enable = true;
		} else {
			/**
			 * 给他添加一个1P控制器
			 */
			role.registerKeyReceiver(new Player1Key());
		}
		var player1 = new PlayerTips(role, this.assets.getBitmapData("player1"));
		this.topBox.addChild(player1);
		player1.scaleX = player1.scaleY = 0.3;

		/**
		 * 创建一个角色
		 */
		var role = new GameRole(this, gameAssets.roles.get(this.option.player2));
		role.troop = 1;
		role.body.x = mapWidth / 2 + 150;
		role.body.y = maxWorldY + 5;
		role.scaleX = -1;
		this.roleBoids.push(role.body);
		this.addDisplay(role);

		if (roleB != null && roleB.data.hp > 0) {
			// 保留HP MP
			role.data = roleB.data;
			if (role.roleid != roleB.roleid) {
				role.switchRole(role.roleid);
			}
		}

		fightState.p2.bindRole(role);
		if (p2AiEnable) {
			role.aiScript = new AiScript();
			role.aiScript.enable = true;
		} else {
			/**
			 * 给他添加一个2P控制器
			 */
			role.registerKeyReceiver(new Player2Key());
		}
		var player2 = new PlayerTips(role, this.assets.getBitmapData("player2"));
		this.topBox.addChild(player2);
		player2.scaleX = player2.scaleY = 0.3;
	}
}
